package main

func main()  {
	//<-chan <-chan interface{}

	//处理一个充满channel的channel，将其拆解为一个简单的channel
	bridge := func(
	done <-chan interface{},
	chanStream <-chan <-chan interface{},
	) <-chan interface{} {
	valStream := make(chan interface{})    //将返回bridge中的所有值的channel
	go func() {
	defer close(valStream)
	for {  //从chanStream中提取channel并将其提供给嵌套循环来使用
	var stream <-chan interface{}
	select {
	case maybeStream,ok := <-chanStream:
	if ok==false {
	return
	}
	stream = maybeStream
	case <-done:
	return
	}
	for val := range orDone(done,stream) { //读取已给出的channel值，并将其重复到valStream中
	select {
	case valStream <- val:
	case <-done:
	}
	}
	}
	}()
	return valStream
	}

	//创建10个channel，每个channel写入一个元素，并将这些channel传递给桥接函数：
	genVals := func() <-chan <-chan interface{} {
	chanStream := make(chan (<-chan interface{}))
	go func() {
	defer close(chanStream)
	for i := 0;i<10;i++ {
	stream := make(chan interface{},1)
	stream <- i
	close(stream)
	chanStream <- stream
	}
	}()
	return chanStream
	}
	for v := range bridge(nil,genVals()) {
	fmt.Printf("%v ",v)
	}



}

